<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="table-container">
          <el-row style="margin-left: 60px;">
            <el-row>
              <el-col :span="12"><span style="color: #909399;">企业名称: </span>{{ name }}</el-col>
              <el-col :span="12"><span style="color: #909399;">法人:</span>{{ legalPerson }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="12"><span style="color: #909399;">注册地址: </span>{{ registeredAddress }}</el-col>
              <el-col :span="12"><span style="color: #909399;">所在行业: </span>{{ industryName }}</el-col>
            </el-row>
            <br>
            <el-col :span="12"><span style="color: #909399;">营业执照: <img :src="businessLicenseUrl" alt="照片显示" style="width: 25%;"> </span></el-col>
          </el-row>
        </div>
      </div>
      <div class="form-container">
        <div class="title">联系人信息</div>
        <div class="table-container">
          <el-row style="margin-left: 60px;">
            <el-row>
              <el-col :span="12"><span style="color: #909399;">企业联系人: </span>{{ contact }}</el-col>
              <el-col :span="12"><span style="color: #909399;">联系电话: </span>{{ contactNumber }}</el-col>
            </el-row>
          </el-row>
        </div>
      </div>
      <div class="form-container">
        <div class="form-container">
          <div class="title">租赁记录</div>
          <div class="table-container">
            <el-table
              :data="form"
              style="width: 100%"
              border
            >
              <el-table-column
                type="index"
                label="序号"
                width="50"
              />
              <el-table-column
                prop="buildingName"
                label="租赁楼宇"
                width="180"
              />
              <el-table-column
                label="租赁起止时间"
                width="280"
              >
                <template #default="{row}">
                  {{ row.startTime }} - {{ row.endTime }}
                </template>
              </el-table-column>
              <el-table-column
                label="租赁合同(点击预览)"
              >
                <template #default="{row}">
                  <el-button type="text">
                    <a :href="row.contractUrl" target="_blank">
                      {{ row.contractName }}
                    </a>
                  </el-button>
                </template>
              </el-table-column>
              <el-table-column
                prop="createTime"
                label="录入时间"
              />
              <el-table-column
                prop="address"
                label="操作"
              >
                <template v-slot="{row}">
                  <el-button type="text" @click="upload(row)">合同下载</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div></main>

  </div>
</template>

<script>
import { downloadContractApi, getEnterpriseDetailAPI } from '@/api/enterprise'
import FileSaver from 'file-saver'

export default {
  data() {
    return {
      form: [],
      name: '',
      contact: '',
      contactNumber: '',
      legalPerson: '',
      registeredAddress: '',
      industryName: '',
      businessLicenseUrl: '',
      previewURL: 'https://view.officeapps.live.com/op/view.aspx?src=' // 预览地址
    }
  },
  computed: {
    // 缓存id 方便调用
    id() {
      return this.$route.params.id
    }
  },
  created() {
    console.log(this.$options)
  },
  mounted() {
    this.getEnterpriseDetail(this.id)
  },
  methods: {
    async getEnterpriseDetail() {
      const { data } = await getEnterpriseDetailAPI(this.id)
      this.form = data.rent
      this.name = data.name
      this.contact = data.contact
      this.contactNumber = data.contactNumber
      this.legalPerson = data.legalPerson
      this.registeredAddress = data.registeredAddress
      this.industryName = data.industryName
      this.businessLicenseUrl = data.businessLicenseUrl
      // 预处理预览地址
      // PDF：直接浏览器打开
      // 在线预览工具只能浏览 doc 文件，需要拼接前缀预览地址
      data.rent.forEach(obj => {
        const url = obj.contractUrl
        // 它使用 slice 方法和 lastIndexOf 方法找到 url 中最后一个 . 的位置，
        // 并从该位置开始截取字符串，将结果存储在 extName 变量中。这是为了获取文件的扩展名
        const extName = url.slice(url.lastIndexOf('.'))
        if (extName === '.doc') {
          obj.contractUrl = this.previewURL + obj.contractUrl
        }
      })
    },
    async upload(row) {
      const res = await downloadContractApi(row.contractId)
      // console.log(res)
      FileSaver.saveAs(res, '企业租赁合同.pdf')
      // 文件流数据 => File对象（父类 Blob）=> 转临时 URL 地址
      // const url = window.URL.createObjectURL(res)
      // const a = document.createElement('a')
      // a.href = url
      // a.download = row.contractName || ''
      // a.click()
    }
  }
}
</script>

  <style scoped lang="scss">
  .detail-enterprise {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      align-items: center;
      padding: 0 20px;
      height: 64px;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;
        margin-bottom: 20px;
        .title {
          height: 60px;
          line-height: 60px;
          padding-left: 20px;
        }
      }
      .table-container{
        padding-top: 20px;
        padding-bottom: 20px;
        padding-left: 35px;
        padding-right: 35px;
      }
      .preview{
        img{
          width: 100px;
        }
      }
    }
  }
  </style>
